package controller;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ListIterator;

import model.Constants;
import model.HostAddress;
import model.IPAddress;
import model.Subnet;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import view.dialogs.FileChooserDialog;
import view.dialogs.InfoDialog;

/**
 * @author Alexander Müller
 * 
 */
public class XmlManager {
	
	private static XmlManager manager;

	private XmlManager() {
	}

	public static synchronized XmlManager getInstance() {
		if (manager == null) {
			manager = new XmlManager();
		}
		return manager;

	}

	public void saveToXML() {

		FileChooserDialog fc = new FileChooserDialog(1);

		if (fc.openDialog()) {

		final Document doc = new Document();

				Element root = new Element(Constants.ROOTELEMENT);


				for (HostAddress hosts : Controller.getInstance().getHosts()) {

					Element hostAddress = new Element(Constants.HOSTELEMENT);
					hostAddress.setAttribute(new Attribute(
							Constants.ATTRIBUTENAME, hosts.getName()));
					hostAddress.setAttribute(new Attribute(
							Constants.ATTRIBUTEADDRESS, hosts.getHostAddress()
									.getAddress()));

					for (Subnet subnets : hosts.getSubnetList()) {

						Element subnet = new Element(Constants.SUBNETELEMENT);
						subnet.setAttribute(new Attribute(
								Constants.ATTRIBUTENAME, subnets.getName()));
						subnet.setAttribute(new Attribute(
								Constants.ATTRIBUTEADDRESS, subnets
										.getNetAdress().getAddress()));
					subnet.setAttribute(new Attribute(Constants.ATTRIBUTESIZE,
							Integer.toString(subnets.getHostSpace())));

						for (IPAddress ipAddresses : subnets.getIpAddressList()) {

							Element ipAddress = new Element(Constants.IPELEMENT);
							Element address = new Element(
									Constants.ADDRESSELEMENT);
							Element additionalInfo = new Element(
									Constants.ADDITIONALELEMENT);

							address.setText(ipAddresses.getAddress());
							additionalInfo.setText(ipAddresses
									.getAdditionalInfo());
							ipAddress.addContent(address);
							ipAddress.addContent(additionalInfo);

							subnet.addContent(ipAddress);

						}

						hostAddress.addContent(subnet);
					}
					root.addContent(hostAddress);
				}
				doc.addContent(root);

		XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());

		try {

			FileWriter fileWriter = new FileWriter(fc.getFilePath());
			xmlOut.output(doc, fileWriter);

			fileWriter.close();

				new InfoDialog(Constants.INFO,
						Constants.SAVESUCCESSMESSAGE);

		} catch (IOException e) {

			e.printStackTrace();
		}
	}
	}


	public void loadFromXML() {

		FileChooserDialog fc = new FileChooserDialog(0);

		if (fc.openDialog()) {

		Document doc = null;
		Controller control = Controller.getInstance();
		ArrayList<HostAddress> hostList = new ArrayList<HostAddress>();

		try {
			
				doc = new SAXBuilder().build(fc.getFilePath());
			
			Element root = doc.getRootElement();
			ListIterator<?> iterator = root.getChildren(Constants.HOSTELEMENT)
					.listIterator();

			while (iterator.hasNext()) {

				Element current = (Element) iterator.next();
				
				HostAddress host = new HostAddress(
						new IPAddress(
								current.getAttributeValue(Constants.ATTRIBUTEADDRESS)),
						current.getAttributeValue(Constants.ATTRIBUTENAME));



				ListIterator<?> subnetIterator = current.getChildren(
						Constants.SUBNETELEMENT).listIterator();

				while (subnetIterator.hasNext()) {

					Element subnetCurrent = (Element) subnetIterator.next();

					Subnet subnet = new Subnet(
							new IPAddress(
									subnetCurrent
											.getAttributeValue(Constants.ATTRIBUTEADDRESS)),Integer.parseInt(subnetCurrent
										.getAttributeValue(Constants.ATTRIBUTESIZE)),
								subnetCurrent
										.getAttributeValue(Constants.ATTRIBUTENAME));

					

					ListIterator<?> ipIterator = subnetCurrent.getChildren(
							Constants.IPELEMENT).listIterator();
					
					while (ipIterator.hasNext()) {
						
						Element ipCurrent = (Element) ipIterator.next();
						
						
						
						for (IPAddress ipAddress : subnet.getIpAddressList()){
							if (ipAddress.getAddress().equalsIgnoreCase(ipCurrent
										.getChildText(Constants.ADDRESSELEMENT))){
								ipAddress.setAdditionalInfo(ipCurrent
										.getChildText(Constants.ADDITIONALELEMENT));
							}
						}
						
					}
					host.getSubnetList().add(subnet);
				}

				hostList.add(host);

			}
			
			control.setHosts(hostList);

				new InfoDialog(Constants.INFO,
						Constants.LOADSUCCESSMESSAGE);

		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		}
	}


}